package com.grad.why.studentsystem.controller;

import com.grad.why.studentsystem.domain.Classinfo;
import com.grad.why.studentsystem.domain.Runtimecoursearrangeinforecord;
import com.grad.why.studentsystem.domain.Student;
import com.grad.why.studentsystem.domain.Studentarrivedinfo;
import com.grad.why.studentsystem.domain.current.CurrentClassArriveInfos;
import com.grad.why.studentsystem.domain.current.CurrentClassinfo;
import com.grad.why.studentsystem.domain.current.CurrentProject;
import com.grad.why.studentsystem.domain.current.CurrentStudentArrivedRecordeAnalyze;
import com.grad.why.studentsystem.service.ClassinfoService;
import com.grad.why.studentsystem.service.RuntimeCourseArrangeInfoRecordService;
import com.grad.why.studentsystem.service.StudentarrivedinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/studentManageSystem")
@Controller
public class StudentArriveInfoController {
    @Autowired
    private RuntimeCourseArrangeInfoRecordService runtimeCourseArrangeInfoRecordService;

    @Autowired
    private StudentarrivedinfoService studentarrivedinfoService;

    @Autowired
    private ClassinfoService classinfoService;

    @RequestMapping("/getCurrentStudentArrivedRecordAnalyze")
    @ResponseBody
    public Object getCurrentStudentArrivedRecordAnalyze(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, defaultValue = "", value = "search") String search
    ) {
        try {
            List<CurrentStudentArrivedRecordeAnalyze> rows = studentarrivedinfoService.getCurrentStudentArrivedRecordAnalyzeList(offset, limit, search);
            long total = studentarrivedinfoService.getCurrentStudentArrivedRecordAnalyzeListLength(search);
            Map<String, Object> result = new HashMap<>(2);
            result.put("rows", rows);
            result.put("total", total);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    @RequestMapping("/getStudentArriveInfos")
    @ResponseBody
    public Object getProjectInfos(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, defaultValue = "", value = "search") String search,
            @RequestParam(required = false, value = "order") String order
    ) {
        try {
            List<Runtimecoursearrangeinforecord> rows = runtimeCourseArrangeInfoRecordService.getStudentArriveInfoList(offset, limit, sort, search);
            for(int i = 0;i<rows.size();i++){
                Runtimecoursearrangeinforecord runtimecoursearrangeinforecord = rows.get(i);
                runtimecoursearrangeinforecord.setRealnamelabels(runtimecoursearrangeinforecord.getRealnamelabels().replaceAll("\\|",","));
            }
            long total = runtimeCourseArrangeInfoRecordService.getStudentArriveInfoListLength(search);
            Map<String, Object> result = new HashMap<>(2);
            result.put("rows", rows);
            result.put("total", total);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/deleteRuntimeCourseArrangeInfoRecordById/{ids}")
    @ResponseBody
    public String deleteRuntimeCourseArrangeInfoRecordById(@PathVariable("ids")String ids){
        try{
            String[] idArrays = ids.split("-");
            for(String s : idArrays){
                int n = runtimeCourseArrangeInfoRecordService.deleteRuntimeCourseArrangeInfoRecordById(Long.parseLong(s));
                if(n<=0){
                    return null;
                }
            }
            return "OK";
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/getNotArrivedStudentNameLabelsByCourseArrangeId/{coursearrangeid}")
    @ResponseBody
    public List<String> getNotArrivedStudentNameLabelsByCourseArrangeId(@PathVariable("coursearrangeid") String coursearrangeid,String arrivedStudentNameLabels){
        try{
            Integer courseArrangeId = Integer.parseInt(coursearrangeid);
            List<String> notArrivedStudentNameLabels = new ArrayList<>();
            List<Student> students = runtimeCourseArrangeInfoRecordService.getShouldArriveStudentNameLabels(courseArrangeId);

            if(!arrivedStudentNameLabels.equals("")) {
                String[] arrivedStudentInfo = arrivedStudentNameLabels.split(",");
                ArrayList<Integer> arrivedStudentIdList = new ArrayList<>();

                for (int i = 0; i < arrivedStudentInfo.length; i++) {
                    arrivedStudentIdList.add(Integer.parseInt(arrivedStudentInfo[i].split("_")[0]));
                }

                for (int i = 0; i < students.size(); i++) {
                    Student s = students.get(i);
                    if (!arrivedStudentIdList.contains(s.getId())) {
                        notArrivedStudentNameLabels.add(s.getId() + "_" + s.getName());
                    }
                }
            }else{
                for (int i = 0; i < students.size(); i++) {
                    Student s = students.get(i);
                    notArrivedStudentNameLabels.add(s.getId() + "_" + s.getName());
                }
            }

            return notArrivedStudentNameLabels;

        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/repairAddStudentArriveInfo/{recordId}")
    @ResponseBody
    public String repairAddStudentArriveInfo(@PathVariable("recordId") String recordId,String info){
        try{
            //修改整体到课信息
            int transformRecordId = Integer.parseInt(recordId);
            Runtimecoursearrangeinforecord runtimecoursearrangeinforecord = runtimeCourseArrangeInfoRecordService.getRuntimeCourseArrangeInfoRecordByRecordId(transformRecordId);
            if(!runtimecoursearrangeinforecord.getRealnamelabels().equals("")){
                runtimecoursearrangeinforecord.setRealnamelabels(runtimecoursearrangeinforecord.getRealnamelabels() + "|" +info);
            }else{
                runtimecoursearrangeinforecord.setRealnamelabels(info);
            }

            runtimecoursearrangeinforecord.setRealcount(runtimecoursearrangeinforecord.getRealcount() + 1);
            int n = runtimeCourseArrangeInfoRecordService.updateRuntimeCourseArrangeInfoRecordByEntity(runtimecoursearrangeinforecord);

            if(n<=0){
                return null;
            }

            String[] infos = info.split("_");
            Integer sid = Integer.parseInt(infos[0]);

            //修改个人到课信息
            Long rId = Long.parseLong(recordId);
            Studentarrivedinfo studentarrivedinfo = studentarrivedinfoService.selectStudentArrivedInfoByRecordIdAndSid(rId,sid);
            studentarrivedinfo.setState(1);
            n = studentarrivedinfoService.updateEntity(studentarrivedinfo);

            if(n<=0){
                return null;
            }else{
                return "OK";
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/getRepairNotArrivedStudentNameLabelsByCourseArrangeId/{recordId}/{courseArrangeId}")
    @ResponseBody
    public List<String> getRepairNotArrivedStudentNameLabelsByCourseArrangeId(@PathVariable("recordId") String recordId,@PathVariable("courseArrangeId") String courseArrangeId){
        try{
            int transformRecordId = Integer.parseInt(recordId);
            Runtimecoursearrangeinforecord runtimecoursearrangeinforecord = runtimeCourseArrangeInfoRecordService.getRuntimeCourseArrangeInfoRecordByRecordId(transformRecordId);
            String realArrivedStudentNameLabels = runtimecoursearrangeinforecord.getRealnamelabels();
            List<String> strings = new ArrayList<>();
            Integer parseCourseArrangeId = Integer.parseInt(courseArrangeId);
            List<Student> students = runtimeCourseArrangeInfoRecordService.getShouldArriveStudentNameLabels(parseCourseArrangeId);

            if(!realArrivedStudentNameLabels.equals("")) {
                String[] realnames = realArrivedStudentNameLabels.split("\\|");
                List<Integer> realArrivedStudentIds = new ArrayList<>();

                for (int i = 0; i < realnames.length; i++) {
                    realArrivedStudentIds.add(Integer.parseInt(realnames[i].split("_")[0]));
                }

                for(Student s : students){
                    if(!realArrivedStudentIds.contains(s.getId())){
                        strings.add(s.getId()+"_"+s.getName());
                    }
                }
            }else{
                for(Student s : students){
                    strings.add(s.getId()+"_"+s.getName());
                }
            }
            return strings;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }


    @RequestMapping("/getCurrentClassArriveInfos")
    @ResponseBody
    public Object getCurrentClassArriveInfos(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, defaultValue = "", value = "search") String search
    ){
        try{
            List<CurrentClassArriveInfos> rows = studentarrivedinfoService.getCurrentClassArriveInfos(offset,limit,search);
            long total = studentarrivedinfoService.getCurrentClassArriveInfosLength(search);
            Map<String, Object> result = new HashMap<>(2);
            result.put("rows", rows);
            result.put("total", total);
            return result;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
